package others.swing.p011;

/*学生成绩管理系统 版本T1.0密码修改界面*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Cpwd extends Frame implements ActionListener {
    Label a1, a2;
    JTextField t1, t2;
    JButton b1, b2;
    String m = "", n = "", sql = "";
    DBAccess db = new DBAccess("SMSystem", "", "");
    CheckEmpty ckt;//判断文本框是否为空

    //z是要访问数据库中的哪个表，s是所要修改的那个用户的ID
    public Cpwd(String z, String s) {
        n = z;
        m = s;
        a1 = new Label("旧密码：");
        a2 = new Label("新密码：");
        t1 = new JTextField(12);
        t2 = new JTextField(12);
        t1.setText("");
        t2.setText("");
        b1 = new JButton("确定");
        b2 = new JButton("取消");
        setLayout(null);

        //设置标签大小和所在位置
        a1.setBounds(30, 50, 60, 35);
        a2.setBounds(30, 90, 60, 35);
        t1.setBounds(90, 50, 120, 35);
        t2.setBounds(90, 90, 120, 35);
        b1.setBounds(130, 150, 70, 35);
        b2.setBounds(220, 150, 70, 35);

        //设置标签内的字体
        b1.setFont(new Font("楷体", 1, 15));//字体大小
        b2.setFont(new Font("楷体", 1, 15));//字体大小

        b1.setForeground(Color.BLUE);//字体颜色
        b2.setForeground(Color.BLUE);//字体颜色

        add(a1);
        add(a2);
        add(t1);
        add(t2);
        add(b1);
        add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        setSize(300, 200);
        setVisible(true);
    }

    //修改密码
    public int update(String p1, String p2) {
        String id[], pwd[];
        int i, j = 0, a = 0;
        db.dbcon();
        try {
            //修改学生的密码
            if (n.equals("student")) {
                sql = "SELECT * FROM student";
                db.dbSelect(sql);
                while (db.rs.next()) {
                    j++;
                }
                id = new String[j];
                pwd = new String[j];
                db.rs.beforeFirst();
                for (i = 0; i < j; i++) {
                    db.rs.next();
                    id[i] = db.rs.getString(2);
                    pwd[i] = db.rs.getString(4);
                }
                for (i = 0; i < j; i++) {
                    if (m.equals(id[i]) && p1.equals(pwd[i])) {
                        a = 1;
                    }
                }
                if (a == 1) {
                    sql = "update student set SPassword='" + p2 + "' where SID='" + m + "'";
                    db.dbUpdate(sql);   //执行更新
                } else {
                    JOptionPane.showMessageDialog(null, "原密码不正确，请重新输入！", "友情提示", 1);
                }
            }
            //修改教师或者教务处用户的密码
            else if (n.equals("teacher")) {
                sql = "SELECT * FROM teacher";
                db.dbSelect(sql);
                while (db.rs.next()) {
                    j++;
                }
                id = new String[j];
                pwd = new String[j];
                db.rs.beforeFirst();
                for (i = 0; i < j; i++) {
                    db.rs.next();
                    id[i] = db.rs.getString(2);
                    pwd[i] = db.rs.getString(4);
                }
                for (i = 0; i < j; i++) {
                    if (m.equals(id[i]) && p1.equals(pwd[i])) {
                        a = 1;
                    }
                }
                if (a == 1) {
                    sql = "update teacher set TPassword='" + p2 + "' where TID='" + m + "'";
                    db.dbUpdate(sql);   //执行更新
                } else {
                    JOptionPane.showMessageDialog(null, "原密码不正确，请重新输入！", "友情提示", 1);
                }
            }
            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            ckt = new CheckEmpty(t1, t2);
            if (ckt.check(0) == true) {
                if (update(t1.getText(), t2.getText()) == 1) {
                    JOptionPane.showMessageDialog(null, "修改成功！\n新密码是：" + t2.getText(), "友情提示", 1);
                }
            }
        } else if (e.getSource() == b2) {
            dispose();//清楚界面  旧版本setVisible(false);
        }
    }
}